package com.yenroc.ho.netty;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtobufIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author： heyanpeng
 * @date： 2021/7/28
 */
public class MessageEncoder extends MessageToByteEncoder<TkMessageVo> {
    @Override
    protected void encode(ChannelHandlerContext ctx, TkMessageVo msg, ByteBuf out) throws Exception {
        LinkedBuffer buffer = LinkedBuffer.allocate(1024);
        Schema<TkMessageVo> schema = RuntimeSchema.getSchema(TkMessageVo.class);
        byte[] array = ProtobufIOUtil.toByteArray(msg, schema, buffer);
        out.writeBytes(array);
    }
}
